package com.example.wannerone.myapplication123;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wannerone.http.HttpPostUtils;

import java.util.HashMap;
import java.util.Map;

public class Main2Activity extends AppCompatActivity {

    private String result;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        final EditText userNameInput = (EditText) findViewById(R.id.editText4);
        final EditText passwordInput = (EditText) findViewById(R.id.editText3);
        Button btn_open = (Button) findViewById(R.id.buttonformake);
        Button btn_login = (Button) findViewById(R.id.btn_back);
        //全局变量
        MyApplication application = (MyApplication) getApplication();
        System.out.println("application.getU_id=" + application.getU_id());


        btn_open.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
                Intent intent = new Intent(Main2Activity.this, makeuserActivity.class);
                //启动意图
                startActivity(intent);
            }
        });


        btn_login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String userName = userNameInput.getText().toString();
                String userPassword = passwordInput.getText().toString();
                final Map<String, Object> userInfo = new HashMap<String, Object>();
                userInfo.put("usercode", userName);
                userInfo.put("password", userPassword);
                String jsonString = JSONObject.toJSONString(userInfo);
                final String info = "info=" + jsonString;
                if (userName != null && !userName.equals("") && userPassword != null && !userPassword.equals("")) {
                    /*这里需要留意的是httpPostUtils请求在Android里面不能放在主线程里面，必须新建一个子线程，然后通过Hanlder把子线程的值传过来更新UI（因为子线程不能直接更改UI）*/
                    new Thread() {
                        public void run() {
                            String response = HttpPostUtils.doPostRequest("/user/login", info);
                            Message message = Message.obtain();
                            message.obj = response;
                            mHanlder.sendMessage(message);
                        }
                    }.start();
                } else {
                    System.out.println("用户名密码不能为空");
                }

//                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
//                Intent intent = new Intent(Main2Activity.this, functionActivity.class);
//                //启动意图
//                startActivity(intent);
            }
        });
    }

    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message message) {
            result = (String) message.obj;
            JSONObject resultJson = JSON.parseObject(result);
            String finalResult = resultJson.getString("result");
            System.out.println("结果是：" + finalResult);
            if (finalResult.equals("ok")) {
                String u_id = resultJson.getString("u_id");
                MyApplication application = (MyApplication) getApplication();
                application.setU_id(Integer.parseInt(u_id));
                System.out.println("application.getU_id=" + application.getU_id());
                System.out.println("登陆成功");
                //建立一个意图,参数为（当前的Activity类对象，需要开的的Activity类）
                Intent intent = new Intent(Main2Activity.this, functionActivity.class);
                //启动意图
                startActivity(intent);
                Main2Activity.this.finish();
            } else
                System.out.println("用户名或密码错误");
        }
    };
}
